package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblVipGoodsMapper;
import com.showsu.editor.entity.doo.VipGoods;
import com.showsu.editor.entity.po.TblVipGoods;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IVipGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 7:29 下午 2024/9/13
 * @Modified By:
 */
@Service
@Slf4j
public class VipGoodsServiceImpl extends ServiceImpl<TblVipGoodsMapper, TblVipGoods> implements IVipGoodsService {

    @Autowired
    private TblVipGoodsMapper tblVipGoodsMapper;

    @Override
    public VipGoods queryByVipLevel(Integer vipLevel) {
        if (vipLevel == null) {
            return null;
        }
        TblVipGoods tblVipGoods = tblVipGoodsMapper.selectOne(new QueryWrapper<TblVipGoods>().lambda()
                .eq(TblVipGoods::getVipLevel, vipLevel)
                .eq(TblVipGoods::getFlag, RowFlagEnum.UnDelete.getCode())
                .last("limit 1"));
        return tblVipGoods == null ? null : BeanUtil.copyProperties(tblVipGoods, VipGoods.class);
    }

    @Override
    public List<VipGoods> queryAll() {
        List<TblVipGoods> tblVipGoodsList = tblVipGoodsMapper.selectList(new QueryWrapper<TblVipGoods>().lambda()
                .eq(TblVipGoods::getFlag, RowFlagEnum.UnDelete.getCode())
                .orderByAsc(TblVipGoods::getId));
        return CollectionUtils.isEmpty(tblVipGoodsList) ? null
                : tblVipGoodsList.stream().map(tblVipGoods -> { return BeanUtil.copyProperties(tblVipGoods, VipGoods.class); }).collect(Collectors.toList());
    }
}
